package com.sicnu.cheer.leaugebar.leaguemodle.activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.sicnu.cheer.generalmodule.activity.BaseActivity;
import com.sicnu.cheer.generalmodule.bean.AlbumBean;
import com.sicnu.cheer.generalmodule.bean.MyUser;
import com.sicnu.cheer.generalmodule.config.Constant;
import com.sicnu.cheer.generalmodule.util.StringUtils;
import com.sicnu.cheer.generalmodule.util.UIHelper;
import com.sicnu.cheer.leaugebar.leaguemodle.R;

import java.util.ArrayList;

import cn.bmob.v3.BmobUser;
import cn.bmob.v3.c.V;
import cn.bmob.v3.listener.SaveListener;

public class CreateAlbumActivity extends BaseActivity implements View.OnClickListener {

    private EditText albumNameET;
    private EditText albumDescribeET;
    private View submitBtn;

    @Override
    public int getContentViewId() {
        return R.layout.activity_create_album;
    }

    @Override
    protected void initView() {
        super.initView();
        findViewById(R.id.back).setOnClickListener(this);
        ((TextView) findViewById(R.id.title)).setText("新建相册");
        submitBtn = findViewById(R.id.title_right_button);
        submitBtn.setVisibility(View.VISIBLE);
        submitBtn.setOnClickListener(this);
        albumNameET = ((EditText) findViewById(R.id.album_name_et));
        albumDescribeET = ((EditText) findViewById(R.id.album_describe_et));
    }

    @Override
    public void onClick(View v) {
        if (v.getId()==R.id.back){
            onBackPressed();
        }else if (v.getId()==R.id.title_right_button){
            check();
        }
    }

    private void check() {
        String name=albumNameET.getText().toString();
        String describe=albumDescribeET.getText().toString();
        if (StringUtils.isEmpty(name)){
            UIHelper.ToastMessage(mThis,"请输入相册名字");
            return;
        }
        submit(name,describe);
    }
    private void submit(String name,String describe) {
        final ProgressDialog progressDialog=new ProgressDialog(mThis);
        progressDialog.setMessage("加载中...");
        progressDialog.show();

        final AlbumBean bean = new AlbumBean();
        bean.setOwner(MyUser.getCurrentUser(mThis,MyUser.class).getLeague());
        bean.setName(name);
        bean.setDescribe(describe);
        bean.setImgUrls(new ArrayList<String>());
        bean.save(mThis, new SaveListener() {
            @Override
            public void onSuccess() {
                progressDialog.dismiss();
                UIHelper.ToastMessage(mThis,"新建相册成功");
                Intent intent=new Intent(mThis,PhotoActivity.class);
                intent.putExtra("bean",bean);
                startActivity(intent);
                setResult(Constant.ALBUM_RESULT_FROM);
                finish();
            }

            @Override
            public void onFailure(int code, String arg0) {
                progressDialog.dismiss();
                UIHelper.ToastMessage(mThis,"新建相册失败:"+arg0);
            }
        });
    }
}
